var categoryId = "9";
var queryObj = {};
var signUpInfo = {};
var scopeRecentActive = [];
var tmp = 0;
queryObj.safeTypeId = common.getParameter("safeTypeId") || 0;
queryObj.year = new Date().getFullYear();
queryObj.month = new Date().getMonth()+1;
if(queryObj.safeTypeId){
    common.setActiveItem(queryObj.safeTypeId);
}
$(".loadSingUp").load("signUp.html");
queryActivity($(""),queryObj);
function queryActivity(that,obj){
    $(that).siblings().removeClass("current");
    $(that).addClass("current");
    if(typeof obj === "object"){
        queryObj = _.extend(queryObj,obj);
    }
    var promise = httpService.get({
        url:url.getActivity,
        data:{
            "categoryId":categoryId,
            "safeTypeId":queryObj.safeTypeId,
            "province":queryObj.province,
            "city":queryObj.city,
            "year":queryObj.year,
            "month":queryObj.month
        }
    });

    promise.then(function(response){
        if(response.success){
            var activeHtml = "";
            var recentActiveHtml = "";
            if(response.newActive && response.newActive.length==0){
                $(".no_new_active").css("display","block");
            }
            $(".info_list").html("");
            _.each(response.newActive,function(active,index){
                var url = active.photoUrl+active.photoName;
                var show = "hide";
                if(index===0){
                    show = "show";
                }
                activeHtml += '<div class="relative mart10 clearfix recent '+ show+' " >'+
                    '<div class="ac_left marl10 relative floatleft">'+
                    '<div class="ac_cover" onclick="activityDetail('+active.id+')"><img src="http://img.duodaohulian.com/img/content/mask_over1.png"></div>'+
                    '<span class="ac_page ac_prev"></span>'+
                    '<span class="ac_page ac_next"></span>'+
                    '<ul class="ac_list clearfix" style="left:0" title="每次挪动-637px">'+
                    '<li><img src="'+url+'"></li>'+
                    '</ul>'+
                    '</div>'+
                    '<div class="ac_right mart10 floatright marr15">'+
                    '<h2 class="font18 mart10">'+active.subject+'</h2>'+
                    '<p class="dot_line mart10"></p>'+
                    '<div class="item_txt mart15">'+
                    '<p class="clearfix"><em class="floatleft">时间</em><span>'+active.activeStartTime+'</span></p>'+
                '<p class="clearfix"><em class="floatleft">地点</em><span>'+active.activeAddress+'</span></p>'+
                    '<p class="clearfix"><em class="floatleft">类型</em><span>'+active.activeType+'</span></p>'+
                    '<p class="clearfix"><em class="floatleft">报名截止时间</em><span>'+active.applyEndTime+'</span></p>'+
                '<p class="clearfix"><em class="floatleft">简介</em><span>'+active.briefDes+'</span></p>'+
                '<div class="mart30 alignright"><a href="javascript:" class="btn btn_more font16 start_sign_up" onclick="startSignUp('+active.id+')">开始报名</a></div>'+
                    '</div>'+
                    '</div>'+
                    '</div>';
            })

            $(".info_list").append(activeHtml);
            $(".info_list .ac_next").off("click").on("click",function(){
                if($(this).parents(".recent").next(".recent").length>0){
                    $(this).parents(".recent").css("display","none").next(".recent").css("display","block");
                }
            });
            $(".info_list .ac_prev").off("click").on("click",function(){
                if($(this).parents(".recent").prev(".recent").length>0){
                    $(this).parents(".recent").css("display","none").prev(".recent").css("display","block");
                }
            });
            var activeDate = [];
            scopeRecentActive = response.recentActive;
           var activeByDate= _.groupBy(response.recentActive,function(active){
                var date = new Date(active.activeStartTime);
               var key = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
                active.date = key;
                return key;
            });
            _.each(activeByDate,function(actives){
                var clanderSameDay = $('<div class="clander_same_day"></div>');
                var recentActiveHtml = "";
                _.each(actives,function(recentActive,index){
                    var url = recentActive.photoUrl+recentActive.photoName;
                    var date = new Date(recentActive.activeStartTime);
                    var time = recentActive.date;
                    var today = new Date().getDate();
                    var newYear = new Date().getFullYear();
                    var newMonth = new Date().getMonth()+1;
                    var newTime = newYear+"-"+newMonth+"-"+today;
                    var applyStartTime = new Date(recentActive.applyStartTime).getTime();
                    var applyEndTime = new Date(recentActive.applyEndTime).getTime();
                    var now = new Date().getTime();
                    var applyFlag = "hide";
                    var applyEndFlag = "hide";
                    var applyNoStartFlag = "hide";
                    if(now >= applyStartTime && now <= applyEndTime){
                        applyFlag = "show";
                    }else if(now < applyStartTime){
                        applyNoStartFlag = "show";
                    }else if(now > applyEndTime){
                        applyEndFlag = "show";
                    }
                    var display = "hide";
                    if(time==newTime){
                        if(index===0){
                            $(".clander_right").css("display","none");
                            display = "show";
                        }
                    }
                    recentActiveHtml+='<div class="clander_right floatright marr10 date_active'+time+" "+recentActive.id+" "+display+'">'+
                        '<div class="c_right_top relative">'+
                        '<div class="cover"><img src="http://img.duodaohulian.com/img/content/mask_over3.png"></div>'+
                        '<span class="ac_page ac_prev"></span>'+
                        '<span class="ac_page ac_next"></span>'+
                        '<p><img src="http://img.duodaohulian.com/img/content/h_img1.jpg"></p>'+
                        '</div>'+
                        '<div class="marl20 mart10 marr20">'+
                        '<h2 class="h2 font18">'+recentActive.subject+'</h2>'+
                        '<p class="dot_line mart15"></p>'+
                        '<div class="item_txt mart15">'+
                        '<p>'+
                        '<em>时间</em><span>'+recentActive.activeStartTime+'</span>'+
                        '<em>地点</em><span>'+recentActive.activeAddress+'</span>'+
                        '</p>'+
                        '<p>'+
                        '<em>类型</em><span>'+recentActive.activeType+'</span>'+
                        '<em>报名截止时间</em><span>'+recentActive.applyEndTime+'</span>'+
                        '</p>'+
                        '<p class="clearfix"><em class="floatleft">简介</em><span>'+recentActive.briefDes+'</span></p>'+
                        '<div class="mart10 alignright"><a href="javascript:" class="btn btn_more font16 '+applyEndFlag+'">已结束</a></div>'+
                        '<div class="mart10 alignright"><a href="javascript:" class="btn btn_more font16 '+applyNoStartFlag+'">报名未开始</a></div>'+
                        '<div class="mart10 alignright"><a href="javascript:" class="btn btn_more font16 start_sign_up '+applyFlag+'" onclick="startSignUp('+recentActive.id+')">开始报名</a></div>'+
                        '</div>'+
                        '</div>'+
                        '</div>';

                })
                clanderSameDay.append(recentActiveHtml);
                $(".has_active").append(clanderSameDay);
            })
            $(".clander tbody").on("click","td",function(e){
                var activeClass = $(e.currentTarget).attr("date-active");
                $(".clander_tab tbody td").removeClass("selectedTd");
                $(e.currentTarget).addClass("selectedTd");
                $(".clander_right").css("display","none");
                if($("."+activeClass).length===0){
                    $(".no_active").css("display","block");
                    return;
                }
                $($("."+activeClass)[0]).css("display","block");
            })

            $(".clander_same_day .ac_next").off("click").on("click",function(){
                if($(this).parents(".clander_right").next(".clander_right").length>0){
                    $(this).parents(".clander_right").css("display","none").next(".clander_right").css("display","block");
                }
            });
            $(".clander_same_day .ac_prev").off("click").on("click",function(){
                if($(this).parents(".clander_right").prev(".clander_right").length>0){
                    $(this).parents(".clander_right").css("display","none").prev(".clander_right").css("display","block");
                }
            });
            $.fn.eCalendar.defaults.eventsInfo = scopeRecentActive;
        }
        if(tmp==0){
            $('.clander tbody').eCalendar();
            tmp=1;
        }

    })
}

function activityDetail(id){
    window.location.href="activityDetails.html?id="+id;
}

function activityCalDetail(){
    window.location.href="activityCalendarDetails.html";
}
$(".choose_place .province ul").on("click","li",function(){
    var value = $(this).children("a").html();
    $(this).parent("ul").prev("p").html(value);
    var cityArr = city[value];
    var cityHtml = "";
    _.each(cityArr,function(city){
        cityHtml += '<li><a href="javascript:;">'+city+'</a></li>';
    })
    $(".city ul").html("").append(cityHtml);
    $(".city p").html(cityArr[0]);
    if(value=="请选择省份名"){
        value = "";
    }
    queryObj.province = value;
    queryObj.city = "";
    $(".clander tbody").html("");
    $('.clander tbody').eCalendar();
    queryActivity($(""),queryObj);
});

//点击市下拉
$(".choose_place .city ul").on("click","li",function(){
    var value = $(this).children("a").html();
    if(value==="请选择城市名"){
        $(this).parent("ul").next("label").addClass("has-error");
    }else{
        $(this).parent("ul").next("label").removeClass("has-error");
    }
    $(this).parent("ul").prev("p").html(value);
    if(value=="请选择城市名"){
        value = "";
    }
    queryObj.city = value;
    $(".clander tbody").html("");
    $('.clander tbody').eCalendar();
    queryActivity($(""),queryObj);
});
common.showProvinceInfo($(".choose_place .province ul"));

